package com.lpf.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * ByteBuf的各种方法
 */
public class ByteBufTest02 {

    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.copiedBuffer("张hello world", CharsetUtil.UTF_8);

        // 如果为true的话就为堆上缓冲
        if (byteBuf.hasArray()) {
            byte[] content = byteBuf.array();
            System.out.println(new String(content, CharsetUtil.UTF_8));

            System.out.println(byteBuf);

            System.out.println("arrayOffset: " + byteBuf.arrayOffset());
            System.out.println("readerIndex: " + byteBuf.readerIndex());
            System.out.println("writerIndex: " + byteBuf.writerIndex());
            System.out.println("capacity: " + byteBuf.capacity());

            // 可读字节数
            int length = byteBuf.readableBytes();
            System.out.println("readableBytes: " + length);

            for (int i = 0; i < byteBuf.readableBytes(); i++) {
                System.out.println((char)byteBuf.getByte(i));
            }

            System.out.println(byteBuf.getCharSequence(0, 4, CharsetUtil.UTF_8));
            System.out.println(byteBuf.getCharSequence(4, 6, CharsetUtil.UTF_8));
        }
    }
}
